<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActionView::Helpers::FormOptionsHelper</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActionView::Helpers::FormOptionsHelper</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/vendor/rails/actionpack/lib/action_view/helpers/form_options_helper_rb.html">
                vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Provides a number of methods for turning different kinds of containers into
a set of option tags.
</p>
<h2>Options</h2>
<p>
The <tt><a
href="FormOptionsHelper.html#M000941">collection_select</a></tt>, <tt><a
href="FormOptionsHelper.html#M000942">country_select</a></tt>, <tt><a
href="FormOptionsHelper.html#M000940">select</a></tt>, and <tt><a
href="FormOptionsHelper.html#M000943">time_zone_select</a></tt> methods
take an <tt>options</tt> parameter, a hash.
</p>
<ul>
<li><tt>:include_blank</tt> - set to true or a prompt string if the first
option element of the <a href="FormOptionsHelper.html#M000940">select</a>
element is a blank. Useful if there is not a default value required for the
<a href="FormOptionsHelper.html#M000940">select</a> element.

</li>
</ul>
<p>
For example,
</p>
<pre>
  select(&quot;post&quot;, &quot;category&quot;, Post::CATEGORIES, {:include_blank =&gt; true})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[category]&quot;&gt;
    &lt;option&gt;&lt;/option&gt;
    &lt;option&gt;joke&lt;/option&gt;
    &lt;option&gt;poem&lt;/option&gt;
  &lt;/select&gt;
</pre>
<p>
Another common case is a <a
href="FormOptionsHelper.html#M000940">select</a> tag for an
<tt>belongs_to</tt>-associated object.
</p>
<p>
Example with @post.person_id =&gt; 2:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank =&gt; 'None'})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;None&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot; selected=&quot;selected&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>
<ul>
<li><tt>:prompt</tt> - set to true or a prompt string. When the <a
href="FormOptionsHelper.html#M000940">select</a> element doesn&#8216;t have
a value yet, this prepends an option with a generic prompt &#8212;
&quot;Please <a href="FormOptionsHelper.html#M000940">select</a>&quot;
&#8212; or the given prompt string.

</li>
</ul>
<p>
Example:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt =&gt; 'Select Person'})
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Select Person&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000941">collection_select</a>&nbsp;&nbsp;
      <a href="#M000947">country_options_for_select</a>&nbsp;&nbsp;
      <a href="#M000942">country_select</a>&nbsp;&nbsp;
      <a href="#M000946">option_groups_from_collection_for_select</a>&nbsp;&nbsp;
      <a href="#M000944">options_for_select</a>&nbsp;&nbsp;
      <a href="#M000945">options_from_collection_for_select</a>&nbsp;&nbsp;
      <a href="#M000940">select</a>&nbsp;&nbsp;
      <a href="#M000948">time_zone_options_for_select</a>&nbsp;&nbsp;
      <a href="#M000943">time_zone_select</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../../ERB/Util.html">ERB::Util</a></span>
      </div>
    </div>

    <div id="section">


    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">COUNTRIES</td>
          <td>=</td>
          <td class="context-item-value">[&quot;Afghanistan&quot;, &quot;Aland Islands&quot;, &quot;Albania&quot;, &quot;Algeria&quot;, &quot;American Samoa&quot;, &quot;Andorra&quot;, &quot;Angola&quot;,           &quot;Anguilla&quot;, &quot;Antarctica&quot;, &quot;Antigua And Barbuda&quot;, &quot;Argentina&quot;, &quot;Armenia&quot;, &quot;Aruba&quot;, &quot;Australia&quot;, &quot;Austria&quot;,           &quot;Azerbaijan&quot;, &quot;Bahamas&quot;, &quot;Bahrain&quot;, &quot;Bangladesh&quot;, &quot;Barbados&quot;, &quot;Belarus&quot;, &quot;Belgium&quot;, &quot;Belize&quot;, &quot;Benin&quot;,           &quot;Bermuda&quot;, &quot;Bhutan&quot;, &quot;Bolivia&quot;, &quot;Bosnia and Herzegowina&quot;, &quot;Botswana&quot;, &quot;Bouvet Island&quot;, &quot;Brazil&quot;,           &quot;British Indian Ocean Territory&quot;, &quot;Brunei Darussalam&quot;, &quot;Bulgaria&quot;, &quot;Burkina Faso&quot;, &quot;Burundi&quot;, &quot;Cambodia&quot;,           &quot;Cameroon&quot;, &quot;Canada&quot;, &quot;Cape Verde&quot;, &quot;Cayman Islands&quot;, &quot;Central African Republic&quot;, &quot;Chad&quot;, &quot;Chile&quot;, &quot;China&quot;,           &quot;Christmas Island&quot;, &quot;Cocos (Keeling) Islands&quot;, &quot;Colombia&quot;, &quot;Comoros&quot;, &quot;Congo&quot;,           &quot;Congo, the Democratic Republic of the&quot;, &quot;Cook Islands&quot;, &quot;Costa Rica&quot;, &quot;Cote d'Ivoire&quot;, &quot;Croatia&quot;, &quot;Cuba&quot;,           &quot;Cyprus&quot;, &quot;Czech Republic&quot;, &quot;Denmark&quot;, &quot;Djibouti&quot;, &quot;Dominica&quot;, &quot;Dominican Republic&quot;, &quot;Ecuador&quot;, &quot;Egypt&quot;,           &quot;El Salvador&quot;, &quot;Equatorial Guinea&quot;, &quot;Eritrea&quot;, &quot;Estonia&quot;, &quot;Ethiopia&quot;, &quot;Falkland Islands (Malvinas)&quot;,           &quot;Faroe Islands&quot;, &quot;Fiji&quot;, &quot;Finland&quot;, &quot;France&quot;, &quot;French Guiana&quot;, &quot;French Polynesia&quot;,           &quot;French Southern Territories&quot;, &quot;Gabon&quot;, &quot;Gambia&quot;, &quot;Georgia&quot;, &quot;Germany&quot;, &quot;Ghana&quot;, &quot;Gibraltar&quot;, &quot;Greece&quot;, &quot;Greenland&quot;, &quot;Grenada&quot;, &quot;Guadeloupe&quot;, &quot;Guam&quot;, &quot;Guatemala&quot;, &quot;Guernsey&quot;, &quot;Guinea&quot;,           &quot;Guinea-Bissau&quot;, &quot;Guyana&quot;, &quot;Haiti&quot;, &quot;Heard and McDonald Islands&quot;, &quot;Holy See (Vatican City State)&quot;,           &quot;Honduras&quot;, &quot;Hong Kong&quot;, &quot;Hungary&quot;, &quot;Iceland&quot;, &quot;India&quot;, &quot;Indonesia&quot;, &quot;Iran, Islamic Republic of&quot;, &quot;Iraq&quot;,           &quot;Ireland&quot;, &quot;Isle of Man&quot;, &quot;Israel&quot;, &quot;Italy&quot;, &quot;Jamaica&quot;, &quot;Japan&quot;, &quot;Jersey&quot;, &quot;Jordan&quot;, &quot;Kazakhstan&quot;, &quot;Kenya&quot;,           &quot;Kiribati&quot;, &quot;Korea, Democratic People's Republic of&quot;, &quot;Korea, Republic of&quot;, &quot;Kuwait&quot;, &quot;Kyrgyzstan&quot;,           &quot;Lao People's Democratic Republic&quot;, &quot;Latvia&quot;, &quot;Lebanon&quot;, &quot;Lesotho&quot;, &quot;Liberia&quot;, &quot;Libyan Arab Jamahiriya&quot;,           &quot;Liechtenstein&quot;, &quot;Lithuania&quot;, &quot;Luxembourg&quot;, &quot;Macao&quot;, &quot;Macedonia, The Former Yugoslav Republic Of&quot;,           &quot;Madagascar&quot;, &quot;Malawi&quot;, &quot;Malaysia&quot;, &quot;Maldives&quot;, &quot;Mali&quot;, &quot;Malta&quot;, &quot;Marshall Islands&quot;, &quot;Martinique&quot;,           &quot;Mauritania&quot;, &quot;Mauritius&quot;, &quot;Mayotte&quot;, &quot;Mexico&quot;, &quot;Micronesia, Federated States of&quot;, &quot;Moldova, Republic of&quot;,           &quot;Monaco&quot;, &quot;Mongolia&quot;, &quot;Montenegro&quot;, &quot;Montserrat&quot;, &quot;Morocco&quot;, &quot;Mozambique&quot;, &quot;Myanmar&quot;, &quot;Namibia&quot;, &quot;Nauru&quot;,           &quot;Nepal&quot;, &quot;Netherlands&quot;, &quot;Netherlands Antilles&quot;, &quot;New Caledonia&quot;, &quot;New Zealand&quot;, &quot;Nicaragua&quot;, &quot;Niger&quot;,           &quot;Nigeria&quot;, &quot;Niue&quot;, &quot;Norfolk Island&quot;, &quot;Northern Mariana Islands&quot;, &quot;Norway&quot;, &quot;Oman&quot;, &quot;Pakistan&quot;, &quot;Palau&quot;,           &quot;Palestinian Territory, Occupied&quot;, &quot;Panama&quot;, &quot;Papua New Guinea&quot;, &quot;Paraguay&quot;, &quot;Peru&quot;, &quot;Philippines&quot;,           &quot;Pitcairn&quot;, &quot;Poland&quot;, &quot;Portugal&quot;, &quot;Puerto Rico&quot;, &quot;Qatar&quot;, &quot;Reunion&quot;, &quot;Romania&quot;, &quot;Russian Federation&quot;,           &quot;Rwanda&quot;, &quot;Saint Barthelemy&quot;, &quot;Saint Helena&quot;, &quot;Saint Kitts and Nevis&quot;, &quot;Saint Lucia&quot;,           &quot;Saint Pierre and Miquelon&quot;, &quot;Saint Vincent and the Grenadines&quot;, &quot;Samoa&quot;, &quot;San Marino&quot;,           &quot;Sao Tome and Principe&quot;, &quot;Saudi Arabia&quot;, &quot;Senegal&quot;, &quot;Serbia&quot;, &quot;Seychelles&quot;, &quot;Sierra Leone&quot;, &quot;Singapore&quot;,           &quot;Slovakia&quot;, &quot;Slovenia&quot;, &quot;Solomon Islands&quot;, &quot;Somalia&quot;, &quot;South Africa&quot;,           &quot;South Georgia and the South Sandwich Islands&quot;, &quot;Spain&quot;, &quot;Sri Lanka&quot;, &quot;Sudan&quot;, &quot;Suriname&quot;,           &quot;Svalbard and Jan Mayen&quot;, &quot;Swaziland&quot;, &quot;Sweden&quot;, &quot;Switzerland&quot;, &quot;Syrian Arab Republic&quot;,           &quot;Taiwan, Province of China&quot;, &quot;Tajikistan&quot;, &quot;Tanzania, United Republic of&quot;, &quot;Thailand&quot;, &quot;Timor-Leste&quot;,           &quot;Togo&quot;, &quot;Tokelau&quot;, &quot;Tonga&quot;, &quot;Trinidad and Tobago&quot;, &quot;Tunisia&quot;, &quot;Turkey&quot;, &quot;Turkmenistan&quot;,           &quot;Turks and Caicos Islands&quot;, &quot;Tuvalu&quot;, &quot;Uganda&quot;, &quot;Ukraine&quot;, &quot;United Arab Emirates&quot;, &quot;United Kingdom&quot;,           &quot;United States&quot;, &quot;United States Minor Outlying Islands&quot;, &quot;Uruguay&quot;, &quot;Uzbekistan&quot;, &quot;Vanuatu&quot;, &quot;Venezuela&quot;,           &quot;Viet Nam&quot;, &quot;Virgin Islands, British&quot;, &quot;Virgin Islands, U.S.&quot;, &quot;Wallis and Futuna&quot;, &quot;Western Sahara&quot;,           &quot;Yemen&quot;, &quot;Zambia&quot;, &quot;Zimbabwe&quot;] unless const_defined?(&quot;COUNTRIES&quot;)</td>
          <td width="3em">&nbsp;</td>
          <td class="context-item-desc">
All the countries included in the country_options output.

</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000941" class="method-detail">
        <a name="M000941"></a>

        <div class="method-heading">
          <a href="#M000941" class="method-signature">
          <span class="method-name">collection_select</span><span class="method-args">(object, method, collection, value_method, text_method, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns <tt>&lt;<a
href="FormOptionsHelper.html#M000940">select</a>&gt;</tt> and
<tt>&lt;option&gt;</tt> tags for the collection of existing return values
of <tt>method</tt> for <tt>object</tt>&#8216;s class. The value returned
from calling <tt>method</tt> on the instance <tt>object</tt> will be
selected. If calling <tt>method</tt> returns <tt>nil</tt>, no selection is
made without including <tt>:prompt</tt> or <tt>:include_blank</tt> in the
<tt>options</tt> hash.
</p>
<p>
The <tt>:value_method</tt> and <tt>:text_method</tt> parameters are methods
to be called on each member of <tt>collection</tt>. The return values are
used as the <tt>value</tt> attribute and contents of each
<tt>&lt;option&gt;</tt> tag, respectively.
</p>
<p>
Example object structure for use with this method:
</p>
<pre>
  class Post &lt; ActiveRecord::Base
    belongs_to :author
  end
  class Author &lt; ActiveRecord::Base
    has_many :posts
    def name_with_initial
      &quot;#{first_name.first}. #{last_name}&quot;
    end
  end
</pre>
<p>
Sample usage (selecting the associated <tt>Author</tt> for an instance of
<tt>Post</tt>, <tt>@post</tt>):
</p>
<pre>
  collection_select(:post, :author_id, Author.find(:all), :id, :name_with_initial, {:prompt =&gt; true})
</pre>
<p>
If <tt>@post.author_id</tt> is already <tt>1</tt>, this would return:
</p>
<pre>
  &lt;select name=&quot;post[author_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;D. Heinemeier Hansson&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;D. Thomas&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;M. Clark&lt;/option&gt;
  &lt;/select&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000941-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000941-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 117</span>
117:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">collection_select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
118:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_collection_select_tag</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
119:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000947" class="method-detail">
        <a name="M000947"></a>

        <div class="method-heading">
          <a href="#M000947" class="method-signature">
          <span class="method-name">country_options_for_select</span><span class="method-args">(selected = nil, priority_countries = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of option tags for pretty much any country in the world.
Supply a country name as <tt>selected</tt> to have it marked as the
selected option tag. You can also supply an array of countries as
<tt>priority_countries</tt>, so that they will be listed above the rest of
the (long) list.
</p>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M000940">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000947-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000947-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 247</span>
247:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">country_options_for_select</span>(<span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">priority_countries</span> = <span class="ruby-keyword kw">nil</span>)
248:         <span class="ruby-identifier">country_options</span> = <span class="ruby-value str">&quot;&quot;</span>
249: 
250:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">priority_countries</span>
251:           <span class="ruby-identifier">country_options</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">priority_countries</span>, <span class="ruby-identifier">selected</span>)
252:           <span class="ruby-identifier">country_options</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;&lt;option value=\&quot;\&quot; disabled=\&quot;disabled\&quot;&gt;-------------&lt;/option&gt;\n&quot;</span>
253:         <span class="ruby-keyword kw">end</span>
254: 
255:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">country_options</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-constant">COUNTRIES</span>, <span class="ruby-identifier">selected</span>)
256:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000942" class="method-detail">
        <a name="M000942"></a>

        <div class="method-heading">
          <a href="#M000942" class="method-signature">
          <span class="method-name">country_select</span><span class="method-args">(object, method, priority_countries = nil, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Return <a href="FormOptionsHelper.html#M000940">select</a> and option tags
for the given object and method, using <a
href="FormOptionsHelper.html#M000947">country_options_for_select</a> to
generate the list of option tags.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000942-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000942-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 122</span>
122:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">country_select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">priority_countries</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
123:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_country_select_tag</span>(<span class="ruby-identifier">priority_countries</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
124:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000946" class="method-detail">
        <a name="M000946"></a>

        <div class="method-heading">
          <a href="#M000946" class="method-signature">
          <span class="method-name">option_groups_from_collection_for_select</span><span class="method-args">(collection, group_method, group_label_method, option_key_method, option_value_method, selected_key = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of <tt>&lt;option&gt;</tt> tags, like <tt><a
href="FormOptionsHelper.html#M000945">options_from_collection_for_select</a></tt>,
but groups them by <tt>&lt;optgroup&gt;</tt> tags based on the object
relationships of the arguments.
</p>
<p>
Parameters:
</p>
<table>
<tr><td valign="top"><tt>collection</tt>:</td><td>An array of objects representing the <tt>&lt;optgroup&gt;</tt> tags

</td></tr>
<tr><td valign="top"><tt>group_method</tt>:</td><td>The name of a method which, when called on a member of <tt>collection</tt>,
returns an array of child objects representing the <tt>&lt;option&gt;</tt>
tags

</td></tr>
<tr><td valign="top"><tt>group_label_method</tt>:</td><td>The name of a method which, when called on a member of <tt>collection</tt>,
returns a string to be used as the <tt>label</tt> attribute for its
<tt>&lt;optgroup&gt;</tt> tag

</td></tr>
<tr><td valign="top"><tt>option_key_method</tt>:</td><td>The name of a method which, when called on a child object of a member of
<tt>collection</tt>, returns a value to be used as the <tt>value</tt>
attribute for its <tt>&lt;option&gt;</tt> tag

</td></tr>
<tr><td valign="top"><tt>option_value_method</tt>:</td><td>The name of a method which, when called on a child object of a member of
<tt>collection</tt>, returns a value to be used as the contents of its
<tt>&lt;option&gt;</tt> tag

</td></tr>
<tr><td valign="top"><tt>selected_key</tt>:</td><td>A value equal to the <tt>value</tt> attribute for one of the
<tt>&lt;option&gt;</tt> tags, which will have the <tt>selected</tt>
attribute set. Corresponds to the return value of one of the calls to
<tt>option_key_method</tt>. If <tt>nil</tt>, no selection is made.

</td></tr>
</table>
<p>
Example object structure for use with this method:
</p>
<pre>
  class Continent &lt; ActiveRecord::Base
    has_many :countries
    # attribs: id, name
  end
  class Country &lt; ActiveRecord::Base
    belongs_to :continent
    # attribs: id, name, continent_id
  end
</pre>
<p>
Sample usage:
</p>
<pre>
  option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
</pre>
<p>
Possible output:
</p>
<pre>
  &lt;optgroup label=&quot;Africa&quot;&gt;
    &lt;option value=&quot;1&quot;&gt;Egypt&lt;/option&gt;
    &lt;option value=&quot;4&quot;&gt;Rwanda&lt;/option&gt;
    ...
  &lt;/optgroup&gt;
  &lt;optgroup label=&quot;Asia&quot;&gt;
    &lt;option value=&quot;3&quot; selected=&quot;selected&quot;&gt;China&lt;/option&gt;
    &lt;option value=&quot;12&quot;&gt;India&lt;/option&gt;
    &lt;option value=&quot;5&quot;&gt;Japan&lt;/option&gt;
    ...
  &lt;/optgroup&gt;
</pre>
<p>
<b>Note:</b> Only the <tt>&lt;optgroup&gt;</tt> and <tt>&lt;option&gt;</tt>
tags are returned, so you still have to wrap the output in an appropriate
<tt>&lt;<a href="FormOptionsHelper.html#M000940">select</a>&gt;</tt> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000946-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000946-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 233</span>
233:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">option_groups_from_collection_for_select</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">group_method</span>, <span class="ruby-identifier">group_label_method</span>, <span class="ruby-identifier">option_key_method</span>, <span class="ruby-identifier">option_value_method</span>, <span class="ruby-identifier">selected_key</span> = <span class="ruby-keyword kw">nil</span>)
234:         <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value str">&quot;&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">options_for_select</span>, <span class="ruby-identifier">group</span><span class="ruby-operator">|</span>
235:           <span class="ruby-identifier">group_label_string</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;group.#{group_label_method}&quot;</span>)
236:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;&lt;optgroup label=\&quot;#{html_escape(group_label_string)}\&quot;&gt;&quot;</span>
237:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_from_collection_for_select</span>(<span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;group.#{group_method}&quot;</span>), <span class="ruby-identifier">option_key_method</span>, <span class="ruby-identifier">option_value_method</span>, <span class="ruby-identifier">selected_key</span>)
238:           <span class="ruby-identifier">options_for_select</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">'&lt;/optgroup&gt;'</span>
239:         <span class="ruby-keyword kw">end</span>
240:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000944" class="method-detail">
        <a name="M000944"></a>

        <div class="method-heading">
          <a href="#M000944" class="method-signature">
          <span class="method-name">options_for_select</span><span class="method-args">(container, selected = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Accepts a container (hash, array, enumerable, your type) and returns a
string of option tags. Given a container where the elements respond to
first and last (such as a two-element array), the &quot;lasts&quot; serve
as option values and the &quot;firsts&quot; as option text. Hashes are
turned into this form automatically, so the keys become &quot;firsts&quot;
and values become lasts. If <tt>selected</tt> is specified, the matching
&quot;last&quot; or element will get the selected option-tag.
<tt>Selected</tt> may also be an array of values to be selected when using
a multiple <a href="FormOptionsHelper.html#M000940">select</a>.
</p>
<p>
Examples (call, result):
</p>
<pre>
  options_for_select([[&quot;Dollar&quot;, &quot;$&quot;], [&quot;Kroner&quot;, &quot;DKK&quot;]])
    &lt;option value=&quot;$&quot;&gt;Dollar&lt;/option&gt;\n&lt;option value=&quot;DKK&quot;&gt;Kroner&lt;/option&gt;

  options_for_select([ &quot;VISA&quot;, &quot;MasterCard&quot; ], &quot;MasterCard&quot;)
    &lt;option&gt;VISA&lt;/option&gt;\n&lt;option selected=&quot;selected&quot;&gt;MasterCard&lt;/option&gt;

  options_for_select({ &quot;Basic&quot; =&gt; &quot;$20&quot;, &quot;Plus&quot; =&gt; &quot;$40&quot; }, &quot;$40&quot;)
    &lt;option value=&quot;$20&quot;&gt;Basic&lt;/option&gt;\n&lt;option value=&quot;$40&quot; selected=&quot;selected&quot;&gt;Plus&lt;/option&gt;

  options_for_select([ &quot;VISA&quot;, &quot;MasterCard&quot;, &quot;Discover&quot; ], [&quot;VISA&quot;, &quot;Discover&quot;])
    &lt;option selected=&quot;selected&quot;&gt;VISA&lt;/option&gt;\n&lt;option&gt;MasterCard&lt;/option&gt;\n&lt;option selected=&quot;selected&quot;&gt;Discover&lt;/option&gt;
</pre>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M000940">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000944-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000944-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 158</span>
158:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">container</span>, <span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>)
159:         <span class="ruby-identifier">container</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-keyword kw">if</span> <span class="ruby-constant">Hash</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">container</span>
160: 
161:         <span class="ruby-identifier">options_for_select</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">inject</span>([]) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">options</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
162:           <span class="ruby-identifier">text</span>, <span class="ruby-identifier">value</span> = <span class="ruby-identifier">option_text_and_value</span>(<span class="ruby-identifier">element</span>)
163:           <span class="ruby-identifier">selected_attribute</span> = <span class="ruby-value str">' selected=&quot;selected&quot;'</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">option_value_selected?</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">selected</span>)
164:           <span class="ruby-identifier">options</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">%(&lt;option value=&quot;#{html_escape(value.to_s)}&quot;#{selected_attribute}&gt;#{html_escape(text.to_s)}&lt;/option&gt;)</span>
165:         <span class="ruby-keyword kw">end</span>
166: 
167:         <span class="ruby-identifier">options_for_select</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
168:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000945" class="method-detail">
        <a name="M000945"></a>

        <div class="method-heading">
          <a href="#M000945" class="method-signature">
          <span class="method-name">options_from_collection_for_select</span><span class="method-args">(collection, value_method, text_method, selected = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of option tags that have been compiled by iterating over
the <tt>collection</tt> and assigning the the result of a call to the
<tt>value_method</tt> as the option value and the <tt>text_method</tt> as
the option text. If <tt>selected</tt> is specified, the element returning a
match on <tt>value_method</tt> will get the selected option tag.
</p>
<p>
Example (call, result). Imagine a loop iterating over each <tt>person</tt>
in <tt>@project.people</tt> to generate an input tag:
</p>
<pre>
  options_from_collection_for_select(@project.people, &quot;id&quot;, &quot;name&quot;)
    &lt;option value=&quot;#{person.id}&quot;&gt;#{person.name}&lt;/option&gt;
</pre>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M000940">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000945-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000945-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 179</span>
179:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">options_from_collection_for_select</span>(<span class="ruby-identifier">collection</span>, <span class="ruby-identifier">value_method</span>, <span class="ruby-identifier">text_method</span>, <span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>)
180:         <span class="ruby-identifier">options</span> = <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
181:           [<span class="ruby-identifier">element</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">text_method</span>), <span class="ruby-identifier">element</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">value_method</span>)]
182:         <span class="ruby-keyword kw">end</span>
183:         <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">options</span>, <span class="ruby-identifier">selected</span>)
184:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000940" class="method-detail">
        <a name="M000940"></a>

        <div class="method-heading">
          <a href="#M000940" class="method-signature">
          <span class="method-name">select</span><span class="method-args">(object, method, choices, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Create a <a href="FormOptionsHelper.html#M000940">select</a> tag and a
series of contained option tags for the provided object and method. The
option currently held by the object will be selected, provided that the
object is available. See <a
href="FormOptionsHelper.html#M000944">options_for_select</a> for the
required format of the choices parameter.
</p>
<p>
Example with @post.person_id =&gt; 1:
</p>
<pre>
  select(&quot;post&quot;, &quot;person_id&quot;, Person.find(:all).collect {|p| [ p.name, p.id ] }, { :include_blank =&gt; true })
</pre>
<p>
could become:
</p>
<pre>
  &lt;select name=&quot;post[person_id]&quot;&gt;
    &lt;option value=&quot;&quot;&gt;&lt;/option&gt;
    &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;David&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;Sam&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
  &lt;/select&gt;
</pre>
<p>
This can be used to provide a default set of options in the standard way:
before rendering the create form, a new model instance is assigned the
default options and bound to @model_name. Usually this model is not saved
to the database. Instead, a second model object is created when the create
request is received. This allows the user to submit a form page more than
once with the expected results of creating multiple records. In addition,
this allows a single partial to be used to generate form inputs for both
edit and create forms.
</p>
<p>
By default, post.person_id is the selected option. Specify :selected =&gt;
value to use a different selection or :selected =&gt; nil to leave all
options unselected.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000940-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000940-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 83</span>
83:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">choices</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
84:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_select_tag</span>(<span class="ruby-identifier">choices</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
85:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000948" class="method-detail">
        <a name="M000948"></a>

        <div class="method-heading">
          <a href="#M000948" class="method-signature">
          <span class="method-name">time_zone_options_for_select</span><span class="method-args">(selected = nil, priority_zones = nil, model = TimeZone)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string of option tags for pretty much any time zone in the world.
Supply a <a href="../../TimeZone.html">TimeZone</a> name as
<tt>selected</tt> to have it marked as the selected option tag. You can
also supply an array of <a href="../../TimeZone.html">TimeZone</a> objects
as <tt>priority_zones</tt>, so that they will be listed above the rest of
the (long) list. (You can use <a
href="../../TimeZone.html#M000028">TimeZone.us_zones</a> as a convenience
for obtaining a list of the US time zones.)
</p>
<p>
The <tt>selected</tt> parameter must be either <tt>nil</tt>, or a string
that names a <a href="../../TimeZone.html">TimeZone</a>.
</p>
<p>
By default, <tt>model</tt> is the <a
href="../../TimeZone.html">TimeZone</a> constant (which can be obtained in
ActiveRecord as a value object). The only requirement is that the
<tt>model</tt> parameter be an object that responds to all, and returns an
array of objects that represent time zones.
</p>
<p>
NOTE: Only the option tags are returned, you have to wrap this call in a
regular HTML <a href="FormOptionsHelper.html#M000940">select</a> tag.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000948-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000948-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 275</span>
275:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">time_zone_options_for_select</span>(<span class="ruby-identifier">selected</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">priority_zones</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">model</span> = <span class="ruby-constant">TimeZone</span>)
276:         <span class="ruby-identifier">zone_options</span> = <span class="ruby-value str">&quot;&quot;</span>
277: 
278:         <span class="ruby-identifier">zones</span> = <span class="ruby-identifier">model</span>.<span class="ruby-identifier">all</span>
279:         <span class="ruby-identifier">convert_zones</span> = <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">list</span><span class="ruby-operator">|</span> <span class="ruby-identifier">list</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span> [ <span class="ruby-identifier">z</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-identifier">z</span>.<span class="ruby-identifier">name</span> ] } }
280: 
281:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">priority_zones</span>
282:           <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">convert_zones</span>[<span class="ruby-identifier">priority_zones</span>], <span class="ruby-identifier">selected</span>)
283:           <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;&lt;option value=\&quot;\&quot; disabled=\&quot;disabled\&quot;&gt;-------------&lt;/option&gt;\n&quot;</span>
284: 
285:           <span class="ruby-identifier">zones</span> = <span class="ruby-identifier">zones</span>.<span class="ruby-identifier">reject</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span> <span class="ruby-identifier">priority_zones</span>.<span class="ruby-identifier">include?</span>( <span class="ruby-identifier">z</span> ) }
286:         <span class="ruby-keyword kw">end</span>
287: 
288:         <span class="ruby-identifier">zone_options</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">options_for_select</span>(<span class="ruby-identifier">convert_zones</span>[<span class="ruby-identifier">zones</span>], <span class="ruby-identifier">selected</span>)
289:         <span class="ruby-identifier">zone_options</span>
290:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000943" class="method-detail">
        <a name="M000943"></a>

        <div class="method-heading">
          <a href="#M000943" class="method-signature">
          <span class="method-name">time_zone_select</span><span class="method-args">(object, method, priority_zones = nil, options = {}, html_options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Return <a href="FormOptionsHelper.html#M000940">select</a> and option tags
for the given object and method, using <a
href="FormOptionsHelper.html#M000948">time_zone_options_for_select</a> to
generate the list of option tags.
</p>
<p>
In addition to the <tt>:include_blank</tt> option documented above, this
method also supports a <tt>:model</tt> option, which defaults to <a
href="../../TimeZone.html">TimeZone</a>. This may be used by users to
specify a different time zone model object. (See <a
href="FormOptionsHelper.html#M000948">time_zone_options_for_select</a> for
more information.)
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000943-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000943-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/actionpack/lib/action_view/helpers/form_options_helper.rb, line 134</span>
134:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">time_zone_select</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-identifier">priority_zones</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span> = {}, <span class="ruby-identifier">html_options</span> = {})
135:         <span class="ruby-constant">InstanceTag</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">method</span>, <span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:object</span>)).<span class="ruby-identifier">to_time_zone_select_tag</span>(<span class="ruby-identifier">priority_zones</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">html_options</span>)
136:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>